<?php
/**
 * The template for displaying content in the single.php template
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<header class="entry-header">
		<h1 class="entry-title"><?php the_title(); ?></h1>
	</header><!-- .entry-header -->
        <hr />
	<div class="entry-content">
                <?php                   
                  $content =  explode('<!--contentpartdivider-->',get_the_content());
                  
                  $check =  trim(strip_tags($content[2]));
                  
                  if(empty($check))
                  {
                    $metaValue = get_post_meta(get_the_ID(), 'category_name', true); 

                    if(!is_null($metaValue) && $metaValue != '')
                    {
                      query_posts( 'category_name=' . $metaValue);
                      
                      while ( have_posts() )
                      {
                        the_post();
                        get_template_part( 'content', 'index' );
                      }

                      // bardzo ważne! inaczej mogą pojawić się dziwaczne błędy związane z użyciem the_post()
                      wp_reset_query();        
                    }                    
                  }
                  else
                  {
                    global $post;
                    $post->post_content = $content[2];
                    setup_postdata($post);

                    the_content();                    
                  }
                ?>
		<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
	</div><!-- .entry-content -->

        <footer class="entry-meta">
              <?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>		
	</footer><!-- .entry-meta -->
</article><!-- #post-<?php the_ID(); ?> -->
